package edu.gatech.gro.model.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import edu.gatech.gro.model.Item;
import edu.gatech.gro.model.ListItem;

public class ItemDao extends DatabaseHandler {

	private static final String TAG = "ITEM_DAO";

	public ItemDao(Context context) {
		super(context);
	}

	@Override
	public void clear() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(ITEM_TABLE, "id > 0", null);
		db.close();
	}

	public ArrayList<Item> getAllItems() {
		ArrayList<Item> list = new ArrayList<Item>();
		String selectQuery = "SELECT  * FROM " + ITEM_TABLE;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		if (cursor.moveToFirst()) {
			do {
				Item item = new ListItem();
				item.setId(cursor.getInt(0));
				item.setItemTypeId(cursor.getInt(1));
				item.setGroceryStoreId(cursor.getInt(2));
				item.setName(cursor.getString(3));
				item.setNameClean(cursor.getString(4));
				item.setBarCode(cursor.getString(5));
				item.setBarCodeHash(cursor.getString(6));
				item.setPrice(cursor.getDouble(7));
				item.setCurrency(cursor.getInt(8));
				item.setCreationTime(cursor.getInt(9));
				item.setLastUpdateTime(cursor.getInt(10));
				item.setDeleteFlag(cursor.getInt(11) == 1);
				list.add(item);
			} while (cursor.moveToNext());
		}
		db.close();

		return list;
	}

	public int count() {
		String countQuery = "SELECT  * FROM " + ITEM_TABLE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();
		return cursor.getCount();
	}

	public boolean saveItem(Item item) {
		if (item == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("itemTypeId", item.getItemTypeId());
		values.put("groceryStoreId", item.getGroceryStoreId());
		values.put("name", item.getName());
		values.put("nameClean", item.getNameClean());
		values.put("barCode", item.getBarCode());
		values.put("barCodeHash", item.getBarCodeHash());
		values.put("price", item.getPrice());
		values.put("currency", item.getCurrency());
		values.put("creationTime", item.getCreationTime());
		values.put("lastUpdateTime", item.getLastUpdateTime());
		values.put("deleteFlag", 0);

		long id = db.insert(ITEM_TABLE, null, values);
		if (id == -1) {
			// TODO: error
			return false;
		} else {
			item.setId((int) id);
		}

		db.close();
		return true;
	}

	public boolean upgrade(ArrayList<Item> items) {
		if (items == null) {
			return false;
		}

		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		for (Item item : items) {
			if (item.isDeleteFlag()) {
				db.delete(ITEM_TABLE, "id = ?", new String[] { String.valueOf(item.getId()) });
			} else {
				values.clear();
				values.put("id", item.getId());
				values.put("itemTypeId", item.getItemTypeId());
				values.put("groceryStoreId", item.getGroceryStoreId());
				values.put("name", item.getName());
				values.put("nameClean", item.getNameClean());
				values.put("barCode", item.getBarCode());
				values.put("barCodeHash", item.getBarCodeHash());
				values.put("price", item.getPrice());
				values.put("currency", item.getCurrency());
				values.put("creationTime", item.getCreationTime());
				values.put("lastUpdateTime", item.getLastUpdateTime());
				values.put("deleteFlag", 0);
				db.replace(ITEM_TABLE, null, values);
			}
		}
		db.close();

		return true;
	}

	public boolean updateListItem(Item item) {
		if (item == null || item.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put("itemTypeId", item.getItemTypeId());
		values.put("groceryStoreId", item.getGroceryStoreId());
		values.put("name", item.getName());
		values.put("nameClean", item.getNameClean());
		values.put("barCode", item.getBarCode());
		values.put("barCodeHash", item.getBarCodeHash());
		values.put("price", item.getPrice());
		values.put("currency", item.getCurrency());
		values.put("lastUpdateTime", item.getLastUpdateTime());
		values.put("deleteFlag", item.isDeleteFlag() ? 1 : 0);

		int r = db.update(ITEM_TABLE, values, "id = ?", new String[] { String.valueOf(item.getId()) });
		db.close();
		return r == 1;
	}

	public boolean deleteListItem(ListItem listItem) {
		if (listItem == null || listItem.getId() < 1) {
			return false;
		}
		SQLiteDatabase db = this.getWritableDatabase();
		int r = db.delete(ITEM_TABLE, "id = ?", new String[] { String.valueOf(listItem.getId()) });
		db.close();
		return r == 1;
	}
}
